package org.example.wax.controller;

import org.example.wax.domain.InventoryBody;
import org.example.wax.domain.LightBody;
import org.example.wax.manager.CabinetManager;
import org.example.wax.result.AjaxResult;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/cabinet")
public class CabinetController {

    @GetMapping(value = "/status")
    public AjaxResult getCabinetStatus() {
        AjaxResult result = AjaxResult.success();
        result.put("status", CabinetManager.getInstance().getStatus());
        return result;
    }

    @PostMapping(value = "/inventory")
    public AjaxResult inventory(@Valid @RequestBody InventoryBody body, BindingResult bindingResult) {
        CabinetManager.getInstance().inventory(body);
        return AjaxResult.success();
    }

    @PostMapping(value = "/open")
    public AjaxResult open(@Valid @RequestBody List<Integer> boxes) {
        CabinetManager.getInstance().open(boxes);
        return AjaxResult.success();
    }

    @PostMapping(value = "/multiLightOn")
    public AjaxResult multiLightON(@RequestBody List<LightBody> bodies, @RequestParam(value = "singleTimes", required = false) Integer times) {
        CabinetManager.getInstance().multiLightOn(bodies, times == null ? 8 : times);
        return AjaxResult.success();
    }

    @PostMapping(value = "/multiLightOff")
    public AjaxResult multiLightOff(@RequestBody List<Integer> boxes) {
        CabinetManager.getInstance().multiLightOff(boxes);
        return AjaxResult.success();
    }
}
